package com.scs.application.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.scs.application.core.entity.DataEntity;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.modules.upgrade.db.annotation.EntityClassAnnotation;
import com.scs.application.modules.upgrade.db.annotation.EntityFieldAnnotation;
import com.scs.application.modules.upgrade.db.annotation.EntityIndexAnnotation;
import com.scs.application.modules.upgrade.db.enums.EntityFieldType;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * <p>
 * 流程日志
 * </p>
 */
@Data
@Accessors(chain = true)
@EntityClassAnnotation(remark = "流程日志表")
@TableName("sys_flow_log")
@EntityIndexAnnotation(ix = {"flow_id", "bus_id"})
public class FlowLog extends DataEntity {


    @EntityFieldAnnotation(
            text = "流程ID",
            type = EntityFieldType.LONG,
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String flowId;

    @EntityFieldAnnotation(
            text = "流程名称",
            length = "30",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String flowName;

    @EntityFieldAnnotation(
            text = "操作名称",
            length = "255",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String action;

    @EntityFieldAnnotation(
            text = "业务ID",
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String busId;


    @EntityFieldAnnotation(
            text = "业务类型",
            length = "",
            not_null = false,
            default_value = "",
            comment = "暂定业务表名"
    )
    private String busType;

    @EntityFieldAnnotation(
            text = "业务主办人",
            length = "255",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String busName;

    @EntityFieldAnnotation(
            text = "开始标志",
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private Boolean flagStart;

    @EntityFieldAnnotation(
            text = "开始时间",
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private Date gmtStart;

    @EntityFieldAnnotation(
            text = "结束标志",
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private Boolean flagEnd;

    @EntityFieldAnnotation(
            text = "结束时间",
            length = "",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private Date gmtEnd;

    @EntityFieldAnnotation(
            text = "审批意见",
            length = "200",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String comment;


    @EntityFieldAnnotation(
            text = "附件地址",
            length = "200",
            not_null = false,
            default_value = "",
            comment = ""
    )
    private String attachmentUrl;


    @EntityFieldAnnotation(
            text = "签名地址",
            length = "200",
            not_null = false,
            default_value = "",
            comment = "签名地址"
    )
    private String signUrl;

    @EntityFieldAnnotation(
            text = "节点是否激活",
            length = "",
            not_null = false,
            default_value = "",
            comment = "同一业务主键同一时间只有一个激活的"
    )
    private Boolean flagActive;

    /**
    * @Description：时间花费
    * @Author wyt
    * @Date 2020/12/20
    */
    @TableField(exist = false)
    private String timeCost;

    /**
     * 获取当前节点耗时（毫秒）
     * @return
     */
    private Long getElapsedMillisecond() {
        if (gmtEnd == null || gmtStart == null) {
            return null;
        }

        return Math.abs(gmtEnd.getTime() - gmtStart.getTime());
    }

    /**
     * 获取格式化之后的时间字符串显示
     *
     * @return
     */
    public String getFormattedElapsedTime() {
        Long elapsedMillisecond = getElapsedMillisecond();
        if(elapsedMillisecond == null){
            return null;
        }
        return DateUtils.formatTime(elapsedMillisecond);
    }


}
